package com.wealth.scsj.p_app.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gigamole.navigationtabstrip.NavigationTabStrip;
import com.wealth.scsj.p_app.R;
import com.wealth.scsj.p_app.adapter.FragmentAdapter;
import com.wealth.scsj.p_app.utils.SkipUtils;
import com.wealth.scsj.p_app.view.fragment.BaseFragment;
import com.wealth.scsj.p_app.view.fragment.GetFragment;
import com.wealth.scsj.p_app.view.fragment.SendFragment;
import com.wealth.scsj.p_app.view.fragment.WaitFragment;

import java.util.ArrayList;

/**
 * 主程序
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

    private TextView toolbar_title_tv;
    private Toolbar toolbar;
    private DrawerLayout drawer;
    private NavigationTabStrip tabStrip;
    private ViewPager mViewPager;
    private FragmentAdapter mainFragmentAdapter;
    private String[] titles = {"待抢单", "待取货", "配送中"};
    private LinearLayout total_ll;
    private RelativeLayout enroll_rl;
    private Button enroll_btn;
    private TextView first_tv;
    private LinearLayout my_center_ll, mypurse_ll, new_fish_ll, setting_ll;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        intiNavigationBar();
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        toolbar_title_tv = findViewById(R.id.toolbar_title_tv);
        drawer = findViewById(R.id.drawer_layout);
        toolbar_title_tv = findViewById(R.id.toolbar_title_tv);
        total_ll = findViewById(R.id.total_ll);
        enroll_rl =findViewById(R.id.enroll_rl);
        enroll_btn =findViewById(R.id.enroll_btn);

        my_center_ll = findViewById(R.id.my_center_ll);
        mypurse_ll = findViewById(R.id.mypurse_ll);
        new_fish_ll = findViewById(R.id.new_fish_ll);
        setting_ll = findViewById(R.id.setting_ll);
    }

    @Override
    protected void initListener() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        total_ll.setOnClickListener(this);
        enroll_btn.setOnClickListener(this);
        my_center_ll.setOnClickListener(this);
        mypurse_ll.setOnClickListener(this);
        new_fish_ll.setOnClickListener(this);
        setting_ll.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        //TODO 默认第一次进入开工页面 需要判断当前手机号是否已开通配送权限。
        // TODO 没有开通的话 显示的就是一个需要报名开通的页面(enroll_rl)，
        // TODO 如果已开通显示的就是抢单接单的页面。隐藏 enroll_rl
        // todo 待完善
        enroll_rl.setVisibility(View.GONE);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void intiNavigationBar() {

        //顶部指示器
        tabStrip = findViewById(R.id.mediator_tab);
        mViewPager = findViewById(R.id.viewpager);
        mViewPager.setOffscreenPageLimit(3);
        ArrayList<BaseFragment> list = new ArrayList<>();

        WaitFragment waitFragment = new WaitFragment();
        GetFragment getFragment = new GetFragment();
        SendFragment sendFragment = new SendFragment();

        list.add(waitFragment);
        list.add(getFragment);
        list.add(sendFragment);

        mainFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(mainFragmentAdapter);
        tabStrip.setViewPager(mViewPager, 0);
        tabStrip.setTitles(titles);
        tabStrip.setStripType(NavigationTabStrip.StripType.LINE);
        tabStrip.setStripGravity(NavigationTabStrip.StripGravity.BOTTOM);
        tabStrip.setTabIndex(0, true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.total_ll:
                SkipUtils.toTotalOrder(MainActivity.this);
                break;
            case R.id.enroll_btn:
                SkipUtils.toEnrollWork(MainActivity.this);
                break;
            case R.id.my_center_ll:
                SkipUtils.toMinePage(MainActivity.this);
                break;
            case R.id.mypurse_ll:
                SkipUtils.toMyPurse(MainActivity.this);
                break;
            case R.id.new_fish_ll:
                SkipUtils.toNoviceTraining(MainActivity.this);
                break;
            case R.id.setting_ll:
                SkipUtils.toSetting(MainActivity.this);
                break;

        }
    }
}
